/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.staf.SnmpTopologyExplorer;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;

import org.xml.sax.InputSource;
import org.xml.sax.helpers.XMLReaderAdapter;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

/**
 *
 * @author aezhov
 */
public class NetScanner extends XMLReaderAdapter {
    boolean inHostSection = false;
    boolean isUp = false;
    int hostsUp = 0;
    String mac = null;
    String ip = null;
    String vendor = null;
    String fsDirectory = null;
    HashMap<String, Node> nodes = new HashMap<String, Node>();

    public NetScanner() throws org.xml.sax.SAXException {

    }

    public NetScanner(String filePath) throws ConfigurationException,
        org.xml.sax.SAXException, java.io.IOException
    {
        File file = null;
        FileInputStream fis = null;

        if (filePath.length() == 0)
            throw new ConfigurationException(Errors.INVALID_FILENAME);

        file = new File(filePath);
        if (!file.exists())
            throw new ConfigurationException(Errors.INVALID_FILENAME);
        if (!file.canRead())
            throw new ConfigurationException(Errors.INSUFFICIENT_RIGHTS);

        try {
            fis = new FileInputStream(file);
        } catch (Exception exc) {

        }
        InputSource xmlIS = new InputSource(fis);
        parse(xmlIS);
    }

    public void setFsDirectory(String fsDirectory) {
        this.fsDirectory = fsDirectory;
    }

    public void add(String xmlFilename) throws ConfigurationException,
            org.xml.sax.SAXException, java.io.IOException
    {
        File file = null;
        FileInputStream fis = null;
        String fsSeparator = System.getProperty("file.separator");
        if (fsDirectory != null)
            xmlFilename = fsDirectory + fsSeparator + xmlFilename;

        if (xmlFilename.length() == 0)
            throw new ConfigurationException(Errors.INVALID_FILENAME);

        file = new File(xmlFilename);
        if (!file.exists())
            throw new ConfigurationException(Errors.INVALID_FILENAME);
        if (!file.canRead())
            throw new ConfigurationException(Errors.INSUFFICIENT_RIGHTS);

        try {
            fis = new FileInputStream(file);
        } catch (Exception exc) {

        }
        InputSource xmlIS = new InputSource(fis);
        parse(xmlIS);
    }

    public Node getNode(String mac) {
        Node node = nodes.get(mac);
        if (node == null) {
            node = new Node(mac, null, null, null);
        }

        return node;
    }

    public void startElement(String uri, String localName, String qName,
            Attributes attrs) throws org.xml.sax.SAXException
    {
        String val = null;

        if (qName.equals("host") && !inHostSection) {
            inHostSection = true;
        } else if (qName.equals("status") && inHostSection) {
            val = attrs.getValue("state");
            if (val.equals("up")) {
                System.out.print("Host info: ");
                hostsUp++; isUp = true;
            }
        } else if (qName.equals("address") && inHostSection) {
            val = attrs.getValue("addrtype");
            if (val.equals("mac")) {
                val = attrs.getValue("addr");
                mac = val.toLowerCase();
                vendor = attrs.getValue("vendor");
                if (vendor == null)
                    vendor = "[UNKNOWN]";
            } else if (val.equals("ipv4")) {
                ip = attrs.getValue("addr");
            }
        }
    }

    public void endElement(String uri, String localName, String qName) {
        if (qName.equals("host") && inHostSection) {
            if (isUp)
                System.out.println(mac + " " + ip + " " + vendor);
            inHostSection = false;
            isUp = false;

            if (mac != null) {
                nodes.put(mac, new Node(mac, ip, vendor, null));
                mac = null;
            }
        }
    }

    public void endDocument() {
        System.out.println("== NUMBER OF HOSTS UP " + hostsUp);
    }
}
